package belleza.relativa

import belleza.relativa.security.User

class EntrenamientoController {

    def index() {
        redirect(action: 'entrenar')
    }

    def transient entrenamientoService
    def transient springSecurityService
    def transient redNeuronalOriginalService
    def entrenarFlow = {
        entrenar {
            action {
                if (!flow.map) {
                    flow.map = [:]
                }
                def user = springSecurityService.getCurrentUser() as User
                def foto = redNeuronalOriginalService.getNumeroImagenEntrenamiento(user?.persona?.sexoBuscado == "F" ? 1 : 0)
                [fotoId: foto]
            }
            on("success").to "entrenamiento"
            on(Exception).to "fin"
        }
        entrenamiento {
            on("continue") {
                flow.map[params.fotoId] = params.puntaje
            }.to "entrenar"
            on("fin").to "fin"
        }

        fin {
            action {
                if (flow.map) {
                    def user = springSecurityService.getCurrentUser() as User
                    if (user) {
                        entrenamientoService.entrenar(flow.map, user)
                    }
                }
            }
            on("success").to "redireccionar"
            on(Exception).to "redireccionar"

        }
        redireccionar {
            redirect(controller: 'login', action: 'index')
        }

    }
}
